package com.huaxin.hxmodulemqtt.common;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class ReflectionUtils {
    public static void setFieldValue(Object target, String fieldName, String valueStr) throws Exception {
        if (target == null || fieldName == null || valueStr == null || valueStr.trim().isEmpty()) {
            return;
        }

        String trimmedValue = valueStr.trim();
        Field field;
        try {
            field = target.getClass().getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            return;
        }

        field.setAccessible(true);
        Class<?> type = field.getType();

        Object convertedValue = null;

        switch (fieldName) {
            case "breaker1Status":
            case "breaker2Status":
            case "breaker3Status":
                convertedValue = "1".equals(trimmedValue) ? "closed" : "open";
                break;
            case "alarmSmoke":
            case "alarmWater":
            case "alarmIntrusion":
                convertedValue = "1".equals(trimmedValue) ? "active" : "normal";
                break;
            default:
                if (type == BigDecimal.class) {
//                    BigDecimal raw = new BigDecimal(trimmedValue);
//                    if (fieldName.contains("voltage") && fieldName.contains("Kv")) {
//                        convertedValue = raw.divide(BigDecimal.valueOf(1000), 3, RoundingMode.HALF_UP);
//                    } else if (fieldName.contains("Power") && fieldName.contains("Kw")) {
//                        convertedValue = raw.divide(BigDecimal.valueOf(1000), 3, RoundingMode.HALF_UP);
//                    } else if (fieldName.contains("Power") && fieldName.contains("Kvar")) {
//                        convertedValue = raw.divide(BigDecimal.valueOf(1000), 3, RoundingMode.HALF_UP);
//                    } else if (fieldName.contains("Power") && fieldName.contains("Kva")) {
//                        convertedValue = raw.divide(BigDecimal.valueOf(1000), 3, RoundingMode.HALF_UP);
//                    } else {
//                        convertedValue = raw.setScale(3, RoundingMode.HALF_UP);
//                    }
                    convertedValue = new BigDecimal(trimmedValue).setScale(3, RoundingMode.HALF_UP);
                } else if (type == Integer.class || type == int.class) {
                    convertedValue = Integer.valueOf(trimmedValue);
                } else if (type == Long.class || type == long.class) {
                    convertedValue = Long.valueOf(trimmedValue);
                } else if (type == Boolean.class || type == boolean.class) {
                    convertedValue = "1".equals(trimmedValue) || "true".equalsIgnoreCase(trimmedValue);
                } else if (type == String.class) {
                    convertedValue = trimmedValue;
                }
        }

        if (convertedValue != null) {
            field.set(target, convertedValue);
        }
    }
}